//
//  YZMBankModel.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/18.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMBankModel.h"
#import "YZMImageService.h"
#import <UIImageView+WebCache.h>

#define DIR_FOR_BANK_ICON    @"dir_for_bank_icon"
#define NAME_FOR_BANK_ICON   @"bank_icon_for_"

@interface YZMBankModel()

@property (nonatomic, strong) NSString *logo;

@end

@implementation YZMBankModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{@"code": @"code",
             @"name": @"bankName",
             @"logo":@"logoUrl",
             @"limit":@"remark",
             };
}

- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error{
    
    self = [super initWithDictionary:dictionaryValue error:error];
    if (self == nil) return nil;
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[URL_BASE_STRING stringByAppendingPathComponent:self.logo]]]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSString *iconName = [NAME_FOR_BANK_ICON stringByAppendingString:self.name];
            
            if ([YZMImageService saveImageToDocument:DIR_FOR_BANK_ICON image:image imageName:iconName]) {
                
                self.logoPath = DIR_FOR_BANK_ICON;
            }
        });
    });
    
    return self;
}

/*获取Icon图片*/
- (UIImage *)iconImage{
    
    return [UIImage imageWithData:[YZMImageService loadImageData:DIR_FOR_BANK_ICON fileName:[NAME_FOR_BANK_ICON stringByAppendingString:self.name] isDir:NO] scale:2.0];
}

@end
